package com.tanguy.user.ui.map.activity

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import com.jakewharton.rxbinding2.widget.RxTextView
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.base.ImmersionTitleActivity
import com.tanguy.user.bean.PlaceShowBean
import com.tanguy.user.custom.CenterImageSpan
import com.tanguy.user.db.AdHistoryManager
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.map.adapter.PlaceShowAdapter
import com.tanguy.user.utlis.extend.click
import com.tanguy.user.utlis.extend.request
import com.tanguy.user.utlis.extend.setText
import org.jetbrains.anko.find
import java.util.concurrent.TimeUnit

/**
 * Created by dab on 2017/11/16 0016 11:14
 * 搜索详细地址
 */
class SearchLocationActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_search_location
    private val content by lazy { find<RecyclerView>(R.id.rv_content) }
    private val placeShowAdapter by lazy { PlaceShowAdapter(mPlaceShowList) }
    private val mPlaceShowList by lazy { ArrayList<PlaceShowBean>() }
    lateinit var cityCode: String
    lateinit var cityName: String
    override fun initView() {
        super.initView()
        cityCode = intent.getStringExtra("cityCode")
        cityName = intent.getStringExtra("cityName")
        title = cityName
        val spanString = SpannableString("icon 清空搜索历史")
        val imgSpan = CenterImageSpan(this, R.mipmap.qingkongjilu)
        spanString.setSpan(imgSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        setText(R.id.tv_clear, spanString)
        content.layoutManager = LinearLayoutManager(this)
        content.adapter = placeShowAdapter

    }

    fun inputtips(string: String) {
        HttpManager.inputtips(string, cityName)
                .request(this, false) {
                    if (it == null) return@request
                    if (it.get("status").asString != "1") return@request
                    mPlaceShowList.clear()
                    val asJsonArray = it.getAsJsonArray("tips")

                    for (i in 0 until asJsonArray.size()) {
                        val jsonElement = asJsonArray[i]
                        val asJsonObject = jsonElement.asJsonObject
                        val name = asJsonObject.get("name")
                        if (name.isJsonArray) break
                        val address = asJsonObject.get("address")
                        if (address.isJsonArray) break
                        val location = asJsonObject.get("location")
                        if (location.isJsonArray) break
                        val split = location.asString.split(",")
                        val placeShowBean = PlaceShowBean().init(name.asString, address.asString, split[1].toDouble(), split[0].toDouble())
                        mPlaceShowList.add(placeShowBean)
                    }
                    placeShowAdapter.notifyDataSetChanged()
                    find<EditText>(R.id.et_input).isFocusable = true
                }
    }

    override fun initEvent() {
        super.initEvent()
        RxTextView.textChanges(find(R.id.et_input))
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe {
                    if (TextUtils.isEmpty(it)) {
                        val locations = AdHistoryManager.getLocations(this, 10, cityCode)
                        placeShowAdapter.setNewData(locations)
                        find<EditText>(R.id.et_input).isFocusable = true
                        return@subscribe
                    }
                    inputtips(it.toString())
                }
        placeShowAdapter.setOnItemClick { _, _, data ->
            AdHistoryManager.saveLocation(this, data, cityCode)
            setResult(Activity.RESULT_OK, Intent()
                    .putExtra(Const.IntentCode.LATITUDE, data.latitude.toDouble())
                    .putExtra(Const.IntentCode.LONGITUDE, data.longitude.toDouble())
            )
            finish()
        }
        click(R.id.tv_clear) {
            AdHistoryManager.clearTable(this)
            val locations = AdHistoryManager.getLocations(this, 10, cityCode)
            placeShowAdapter.setNewData(locations)
            find<View>(R.id.tv_clear).visibility = View.GONE
        }
    }

    override fun initData() {
        super.initData()
        val locations = AdHistoryManager.getLocations(this, 10, cityCode)
        if (locations.isEmpty()) {
            find<View>(R.id.tv_clear).visibility = View.GONE
        } else {
            find<View>(R.id.tv_clear).visibility = View.VISIBLE
        }
        placeShowAdapter.setNewData(locations)
    }
}